def read_lines(file)
  File.open(file, 'r') do |ins|
    contents = ins.readlines()
  end
end

def write_lines(file, contents)
  fmt = get_format(contents)
  File.open(file, 'w+') do |outs|
    contents.each_with_index do |str, idx|
      outs.puts("#{sprintf(fmt, idx + 1)} #{str}")
    end
  end
end

def get_format(contents)
  "%0#{contents.size.to_s.size}d"
end

def add_line_num(files=[])
  files.each do |file|
    contents = read_lines(file)
    write_lines(file, contents)    
  end
end

add_line_num(ARGV)
